import * as index from '@/api/index.js'
import {
	baseURL
} from '../../common/config.js'
export default {
	data() {
		return {
			form: {
				address: '', //地址
				longitude: '',
				latitude: '',
				repair_type_id: '', //维修类型id
				car_images: [], //全景图片
				fault_images: [], //故障图片
				remark: '', //备注说明
				phone: uni.getStorageSync('userInfo').mobile || ''
			},
			deposit: '',
			typeList: [],
			typeIndex: -1,
			text:'',
			menuButtonInfo:uni.getMenuButtonBoundingClientRect(),
			background:'rgba(255, 132, 0, 0)'
		}
	},

	onShow() {

	},
	onLoad(option) {
		let form = uni.getStorageSync('form')
		this.form.address = form.address
		this.form.longitude = form.longitude
		this.form.latitude = form.latitude
		this.getRepairList()
		this.getDeposit()
	},
	onPageScroll(e){
		if(e.scrollTop<150){
			this.background=`rgba(255, 132, 0, ${e.scrollTop/150})`
		}
		else{
			this.background='rgba(255, 132, 0, 1)'
		}
	},
	methods: {
		chooseLocation() {
			let that = this
			uni.chooseLocation({
				success: function(res) {
					that.form.address = res.address
					uni.setStorage({
						key: 'form',
						data: that.form,
						success: function() {
							uni.$emit('location', {
								msg: '页面更新'
							})
						}
					});

				}
			});

		},
		//选择维修类型
		chooseType(item, index) {
			this.form.repair_type_id = item.id
			this.typeIndex = index
			this.text = item.text
		},
		//上传图片
		chooseImage(e) {
			let that = this
			uni.chooseMedia({
				count: 9,
				mediaType: ['image'],
				sourceType: ['album', 'camera'],
				maxDuration: 30,
				camera: 'back',
				success(res) {
					if (e == 1) {
						res.tempFiles.forEach(item => {
							uni.uploadFile({
								url: baseURL + 'common/upload', //仅为示例，非真实的接口地址
								filePath: item.tempFilePath,
								name: 'file',
								formData: {
									'user': 'test',
									'Content-type': 'application/json',
									'token': uni.getStorageSync('token'),
								},
								success(res) {
									that.form.car_images.push(JSON.parse(res.data).data.fullurl)

								}
							})
						})
					} else if (e == 2) {
						res.tempFiles.forEach(item => {
							uni.uploadFile({
								url: baseURL + 'common/upload', //仅为示例，非真实的接口地址
								filePath: item.tempFilePath,
								name: 'file',
								formData: {
									'user': 'test',
									'Content-type': 'application/json',
									'token': uni.getStorageSync('token'),
								},
								success(res) {
									that.form.fault_images.push(JSON.parse(res.data).data.fullurl)
								}
							})
						})
					}


				}
			})
		},
		remove(item, index, num) {
			if (num == 1) {
				this.removeByValue(this.form.car_images, item)
			} else if (num == 2) {
				this.removeByValue(this.form.fault_images, item)
			}
		},
		removeByValue(arr, val) {
			for (var i = 0; i < arr.length; i++) {
				if (arr[i] == val) {
					arr.splice(i, 1);
					break;
				}
			}
		},
		getRepairList() {
			index.getRepairList().then(res => {
				this.typeList = res.data
			})
		},
		//定金价格
		getDeposit() {
			index.getDeposit().then(res => {
				this.deposit = Number(res.data)
			})
		},
		back(){
			this.$tools.back(1)
		},
		submit() {
			let that = this
			let form = {}
			form.repair_type_id = this.form.repair_type_id
			form.car_images = JSON.parse(JSON.stringify(this.form.car_images)).join(',')
			form.fault_images = JSON.parse(JSON.stringify(this.form.fault_images)).join(',')
			form.remark = this.form.remark
			form.phone = this.form.phone
			form.longitude = this.form.longitude
			form.latitude = this.form.latitude
			form.address = this.form.address
			uni.showModal({
				title: '提示',
				content: '是否确认支付订单',
				success: function(res) {
					if (res.confirm) {
						index.createOrder(form).then(res => {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success: res => {
									that.$tools.toast('支付成功')
									uni.$emit('update', {
										msg: '页面更新'
									})
									that.$tools.navigate('/pages/order/index',true)
								},
								fail: err => {
									uni.$emit('update', {
										msg: '页面更新'
									})
									that.$tools.toast('支付失败')
								}
							});
						})
					} else if (res.cancel) {
					}
				}
			});

		}
	}
}